#include "Timer8BitPWM.h"

void Timer8BitPWM::setForceOutputCompare(unsigned char flag)
{
	TCCR0 = SetBit(TCCR0, FOC0, flag);
}

unsigned long int Timer8BitPWM::getFrequency(unsigned int prescaler,
					unsigned long int ClK_IO,
					unsigned long int OCR_Count
					)
{
	unsigned long int f = 0;
	double val = 0;
	val = CLK_IO / (2.0 * prescaler * (1 + OCR_Count));
	f = (unsigned long int) val;
	return f;
}

unsigned long int Timer8BitPWM::getPWMFrequency(unsigned int N,
						unsigned long int
						CLK_IO)
{
	unsigned long int f = 0;
	double val = 0;
	val = CLK_IO / (N * 256.0);
	f = (unsigned long int) val;
	return f;
}

unsigned long int Timer8BitPWM::getPWM_PhaseCorrectFrequency(
					unsigned int N,
					unsigned long int CLK_IO)
{
	unsigned long int f = 0;
	double val = 0;
	val = CLK_IO / (N * 510.0);
	f = (unsigned long int) val;
	return f;
}


void Timer8BitPWM::setWaveformGenerationMode(unsigned char flag)
{
	switch(flag)
	{
		case NormalWaveformGeneration:
			TCCR0 = SetBit(TCCR0, CTC0, 0);
			TCCR0 = SetBit(TCCR0, PWM0, 0);
			break;
		case PWM_Phase_CorrectWaveformGeneration:
			TCCR0 = SetBit(TCCR0, CTC0, 0);
			TCCR0 = SetBit(TCCR0, PWM0, 1);
			break;
		case CTC_WaveformGeneration:
			TCCR0 = SetBit(TCCR0, CTC0, 1);
			TCCR0 = SetBit(TCCR0, PWM0, 0);
			break;
		case Fast_PWM_WaveformGeneration:
			TCCR0 = SetBit(TCCR0, CTC0, 1);
			TCCR0 = SetBit(TCCR0, PWM0, 1);
			break;
		default:
			TCCR0 = SetBit(TCCR0, CTC0, 0);
			TCCR0 = SetBit(TCCR0, PWM0, 0);
			break;
	}
}


void Timer8BitPWM::setCompareMatchOutputMode(unsigned char flag)
{
	switch(flag)
	{
		case NormalPortOperation_OC0_Disconnected:
			TCCR0 = SetBit(TCCR0, COM01, 0);
			TCCR0 = SetBit(TCCR0, COM00, 0);
			break;
		case ToggleOC0onCompareMatch:
			TCCR0 = SetBit(TCCR0, COM01, 0);
			TCCR0 = SetBit(TCCR0, COM00, 1);
			break;
		case ClearOC0onCompareMatch:
			TCCR0 = SetBit(TCCR0, COM01, 1);
			TCCR0 = SetBit(TCCR0, COM00, 0);
			break;
		case SetOC0onCompareMatch:
			TCCR0 = SetBit(TCCR0, COM01, 1);
			TCCR0 = SetBit(TCCR0, COM00, 1);
			break;
		default:
			TCCR0 = SetBit(TCCR0, COM01, 0);
			TCCR0 = SetBit(TCCR0, COM00, 0);
			break;
	}
}



void Timer8BitPWM::setClockSelect(unsigned char flag)
{
	switch(flag)
	{
		case NoClockSourceTimer8Bit:
			TCCR0 = SetBit(TCCR0,CS02, 0);
			TCCR0 = SetBit(TCCR0, CS01, 0);
			TCCR0 = SetBit(TCCR0, CS00, 0);
			break;
		case NoPrescalingTimer8Bit:
			TCCR0 = SetBit(TCCR0,CS02, 0);
			TCCR0 = SetBit(TCCR0, CS01, 0);
			TCCR0 = SetBit(TCCR0, CS00, 1);
			break;
		case Prescaler8Timer8Bit:
			TCCR0 = SetBit(TCCR0,CS02, 0);
			TCCR0 = SetBit(TCCR0, CS01, 1);
			TCCR0 = SetBit(TCCR0, CS00, 0);
			break;
		case Prescaler64Timer8Bit:
			TCCR0 = SetBit(TCCR0,CS02, 0);
			TCCR0 = SetBit(TCCR0, CS01, 1);
			TCCR0 = SetBit(TCCR0, CS00, 1);
			break;
		case Prescaler256Timer8Bit:
			TCCR0 = SetBit(TCCR0,CS02, 1);
			TCCR0 = SetBit(TCCR0, CS01, 0);
			TCCR0 = SetBit(TCCR0, CS00, 0);
			break;
		case Prescaler1024Timer8Bit:
			TCCR0 = SetBit(TCCR0,CS02, 1);
			TCCR0 = SetBit(TCCR0, CS01, 0);
			TCCR0 = SetBit(TCCR0, CS00, 1);
			break;
		case ExternalClockFallingTimer8Bit:
			TCCR0 = SetBit(TCCR0,CS02, 1);
			TCCR0 = SetBit(TCCR0, CS01, 1);
			TCCR0 = SetBit(TCCR0, CS00, 0);
			break;
		case ExternalClockRisingTimer8Bit:
			TCCR0 = SetBit(TCCR0,CS02, 1);
			TCCR0 = SetBit(TCCR0, CS01, 1);
			TCCR0 = SetBit(TCCR0, CS00, 1);
			break;
		default:
			TCCR0 = SetBit(TCCR0,CS02, 0);
			TCCR0 = SetBit(TCCR0, CS01, 0);
			TCCR0 = SetBit(TCCR0, CS00, 0);
			break;
	
	}
}


void Timer8BitPWM::setCounterRegister(unsigned char flag) //TCNT
{
	TCNT0 = flag;
}

unsigned char Timer8BitPWM::getCounterRegister()
{
	return TCNT0;
}


void Timer8BitPWM::setOutputCompare(unsigned char flag)
{
	OCR0 = flag;
}

unsigned char Timer8BitPWM::getOutputCompare()
{
	return OCR0;
}


void Timer8BitPWM::setCounter0OutputCompareMatchInterruptEnable(unsigned char flag)
{
	TIMSK = SetBit(TIMSK, OCIE0, flag);
}

void Timer8BitPWM::setCounter0OverflowInterruptEnable(unsigned char flag)
{
	TIMSK = SetBit(TIMSK, TOIE0, flag);
}

unsigned char Timer8BitPWM::getOutputCompareFlag0()
{
	return GetBit(TIFR, OCF0);
}

unsigned char Timer8BitPWM::getOutputCompareOverflow0()
{
	return GetBit(TIFR, TOV0);
}

void Timer8BitPWM::setPrescalerReset(unsigned char flag)
{
	SFIOR = SetBit(SFIOR, PSR10, flag);
}






